package com.huawei.esdk.tpoa.northcommu.cxf.professional;

import javax.xml.datatype.XMLGregorianCalendar;
import org.apache.log4j.Logger;
import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.tp.domain.model.PeriodicalConference;
import com.huawei.esdk.tp.domain.model.ScheduleConference;
import com.huawei.esdk.tp.loggermgr.InterfaceOperationType;
import com.huawei.esdk.tp.loggermgr.LoggerMgr;
import com.huawei.esdk.tpoa.business.professional.confmgr.ConfManagerService;
import com.huawei.esdk.tpoa.northcommu.cxf.professional.convert.TPOAConfMgrConvert;
import com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.DelScheduledConf;
import com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.DelScheduledConfResponse;
import com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.EditRecurrenceConference;
import com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.EditRecurrenceConferenceResponse;
import com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.EditScheduledConf;
import com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.EditScheduledConfResponse;
import com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.ScheduleConf;
import com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.ScheduleConfResponse;
import com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.ScheduleRecurrenceConference;
import com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.ScheduleRecurrenceConferenceResponse;
import com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.TPOAConfMgr;

/** * @author w00208247 * * */
public class TPOAConfMgrImpl implements TPOAConfMgr
{

    private ConfManagerService tpoaConfManagerService;

    private static final Logger LOGGER = Logger
            .getLogger(TPOAConfMgrImpl.class);

    private TPOAConfMgrConvert tpoaConfMgrConvertService;


    /**
     * * @see com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.
     * TPProfessionalConfMgr
     * #editRecurrenceConference(com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.EditRecurrenceConference)
     */
    @Override
    public EditRecurrenceConferenceResponse editRecurrenceConference(
            EditRecurrenceConference parameters)
    {
        SDKResult<PeriodicalConference> result = new SDKResult<PeriodicalConference>();
        EditRecurrenceConferenceResponse response = new EditRecurrenceConferenceResponse();
        try
        {
            if (null == parameters || null == parameters.getEditConf() || null == parameters.getEditConf().getSites())
            {
                 response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                 LoggerMgr.interfacelogger(InterfaceOperationType.MODIFY, 
                     "editRecurrenceConference",
                     parameters, response.getResultCode());
                 return response;
            }
            
            PeriodicalConference periodicalConference = tpoaConfMgrConvertService
                    .editScheduledRecurrenceConfSoap2model(parameters);
            periodicalConference.setSites(
            		tpoaConfMgrConvertService.convertSitesFrom365(periodicalConference.getSites()));
            
            XMLGregorianCalendar editConfBeginTime = DateUtils.setTimezoneToUTC(parameters.getBeginTime(), 0); 
            result = tpoaConfManagerService
                    .editScheduleRecurrenceConfResponseService(periodicalConference, 
                        DateUtils.toDate(editConfBeginTime));
            // 将领域bean的返回值改成北向soapbean的返回值
            response = tpoaConfMgrConvertService
                    .editScheduledRecurrenceConfModel2soap(result);
            if(0 == response.getResultCode())
            {
                response.getRecurrenceConfInfo().setName(periodicalConference.getName());
            }
        }
        catch (SDKException e)
        {
            LOGGER.error("EditRecurrenceConference method SDK error", e);
            result.setErrCode(e.getSdkErrCode());
            response.setResultCode((int) result.getErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("EditRecurrenceConference method SDK error", e);
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            response.setResultCode((int) result.getErrCode());
        }
        
        LoggerMgr.interfacelogger(InterfaceOperationType.MODIFY, 
            "editRecurrenceConference",
            parameters, response.getResultCode());
        return response;
    }


    /**
     * * @see com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.
     * TPProfessionalConfMgr
     * #delScheduledConf(com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.DelScheduledConf)
     */
    @Override
    public DelScheduledConfResponse delScheduledConf(
            DelScheduledConf parameters)
    {
        DelScheduledConfResponse response = new DelScheduledConfResponse();
        try
        {
            if (null == parameters)
            {
                 response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                 LoggerMgr.interfacelogger(InterfaceOperationType.DELETE, 
                     "delScheduledConf",
                     parameters, response.getResultCode());
                 return response;
            }
            
            XMLGregorianCalendar delConfBeginTime = DateUtils.setTimezoneToUTC(parameters.getBeginTime(), 0); 
            SDKErrorCode errorCode = tpoaConfManagerService.delScheduledConf(
                    parameters.getConfId(), delConfBeginTime);
            response.setResultCode((int) errorCode.getErrCode());
        }
        catch (SDKException ex)
        {
            LOGGER.error("delete conference error", ex);
            response.setResultCode((int) ex.getSdkErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("delete conference error", e);
            response.setResultCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        
        LoggerMgr.interfacelogger(InterfaceOperationType.DELETE, 
            "delScheduledConf",
            parameters, response.getResultCode());
        return response;
    }


    /**
     * * @see com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.
     * TPProfessionalConfMgr
     * #scheduleConf(com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.ScheduleConf)
     */
    @Override
    public ScheduleConfResponse scheduleConf(ScheduleConf parameters)
    {
        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        ScheduleConfResponse response = new ScheduleConfResponse();
        try
        {
            if (null == parameters || null == parameters.getScheduleConf() || null == parameters.getScheduleConf().getSites())
            {
                 response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                 LoggerMgr.interfacelogger(InterfaceOperationType.ADD, 
                     "scheduleConf",
                     parameters, response.getResultCode());
                 return response;
            }
            
            ScheduleConference scheduleConference = tpoaConfMgrConvertService
                    .scheduleConfSoap2model(parameters);
            scheduleConference.setSites(
            		tpoaConfMgrConvertService.convertSitesFrom365(scheduleConference.getSites()));
            
            result = tpoaConfManagerService
                    .scheduleConfResponseService(scheduleConference);
            // 将领域bean的返回值改成北向soapbean的返回值
            response = tpoaConfMgrConvertService
                    .scheduleConferenceModel2soap(result);
            
            if(0 == response.getResultCode())
            {
                response.getConferenceInfo().setName(scheduleConference.getName());
            }
        }catch (SDKException e) {
            LOGGER.error("ScheduleConference method error", e);
            result.setErrCode(e.getSdkErrCode());
            response.setResultCode((int) result.getErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("ScheduleConference method error", e);
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            response.setResultCode((int) result.getErrCode());
        }
        
        LoggerMgr.interfacelogger(InterfaceOperationType.ADD, 
            "scheduleConf",
            parameters, response.getResultCode());
        return response;
    }

    public ConfManagerService getTpoaConfManagerService()
    {
        return tpoaConfManagerService;
    }


    public void setTpoaConfManagerService(ConfManagerService tpoaConfManagerService)
    {
        this.tpoaConfManagerService = tpoaConfManagerService;
    }

    public TPOAConfMgrConvert getTpoaConfMgrConvertService() {
		return tpoaConfMgrConvertService;
	}


	public void setTpoaConfMgrConvertService(
			TPOAConfMgrConvert tpoaConfMgrConvertService) {
		this.tpoaConfMgrConvertService = tpoaConfMgrConvertService;
	}


	/**
     * * @see com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.
     * TPProfessionalConfMgr
     * #editScheduledConf(com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.EditScheduledConf)
     */
    @Override
    public EditScheduledConfResponse editScheduledConf(
            EditScheduledConf parameters)
    {
        SDKResult<ScheduleConference> result = new SDKResult<ScheduleConference>();
        EditScheduledConfResponse response = new EditScheduledConfResponse();
        try
        {
            if (null == parameters || null == parameters.getEditConf() || null == parameters.getEditConf().getSites())
            {
                 response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                 LoggerMgr.interfacelogger(InterfaceOperationType.MODIFY, 
                     "editScheduledConf",
                     parameters, response.getResultCode());
                 return response;
            }
            
            ScheduleConference scheduleConference = tpoaConfMgrConvertService
                    .editScheduledConfSoap2model(parameters);
            scheduleConference.setSites(
            		tpoaConfMgrConvertService.convertSitesFrom365(scheduleConference.getSites()));
            
            result = tpoaConfManagerService
                    .editScheduleConfResponseService(scheduleConference);
            // 将领域bean的返回值改成北向soapbean的返回值
            response = tpoaConfMgrConvertService
                    .editScheduledConfModel2soap(result);
            if(0 == response.getResultCode())
            {
                response.getConferenceInfo().setName(scheduleConference.getName());
            }
        }
        catch (SDKException e)
        {
            LOGGER.error("EditConference method SDK error", e);
            result.setErrCode(e.getSdkErrCode());
            response.setResultCode((int) result.getErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("EditConference method SDK error", e);
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            response.setResultCode((int) result.getErrCode());
        }
        
        LoggerMgr.interfacelogger(InterfaceOperationType.MODIFY, 
            "editScheduledConf",
            parameters, response.getResultCode());
        return response;
    }

    /**
     * * @see com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.
     * TPProfessionalConfMgr
     * #scheduleRecurrenceConference(com.huawei.esdk.tpoa.northcommu.cxf.professional.gen.ScheduleRecurrenceConference)
     */
    @Override
    public ScheduleRecurrenceConferenceResponse scheduleRecurrenceConference(
            ScheduleRecurrenceConference parameters)
    {
        // PeriodicalConference,周期会议
        SDKResult<PeriodicalConference> result = new SDKResult<PeriodicalConference>();
        ScheduleRecurrenceConferenceResponse response = new ScheduleRecurrenceConferenceResponse();
        try
        {
            if (null == parameters || null == parameters.getScheduleConf() || null == parameters.getScheduleConf().getSites())
            {
                 response.setResultCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE); 
                 LoggerMgr.interfacelogger(InterfaceOperationType.ADD, 
                     "scheduleRecurrenceConference",
                     parameters, response.getResultCode());
                 return response;
            }
            PeriodicalConference periodicalConference = tpoaConfMgrConvertService
                    .scheduleRecurrenceConfSoap2model(parameters);
            periodicalConference.setSites(
            		tpoaConfMgrConvertService.convertSitesFrom365(periodicalConference.getSites()));
            
            result = tpoaConfManagerService
                    .scheduleRecurrenceConfResponseService(periodicalConference);
            // 将领域bean的返回值改成北向soapbean的返回值
            response = tpoaConfMgrConvertService
                    .scheduleRecurrenceConfModel2soap(result);
            if(0 == response.getResultCode())
            {
                response.getRecurrenceConfInfo().setName(periodicalConference.getName());
            }
        }
        catch (SDKException e)
        {
            LOGGER.error("ScheduleRecurrenceConference method SDK error", e);
            result.setErrCode(e.getSdkErrCode());
            response.setResultCode((int) result.getErrCode());
        }
        catch (Exception e)
        {
            LOGGER.error("ScheduleRecurrenceConference method SDK error", e);
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
            response.setResultCode((int) result.getErrCode());
        }
        
        LoggerMgr.interfacelogger(InterfaceOperationType.ADD, 
            "scheduleRecurrenceConference",
            parameters, response.getResultCode());
        
        return response;
    }

}
